package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.domain.Address;
import com.itheima.domain.ShoppingCart;
import com.itheima.mapper1.AddressMapper;
import com.itheima.service.AddressService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author wuyiqiang
 * @2022/6/19 15:31
 */
@RestController
@RequestMapping(value = "/addressBook", produces = "application/json;charset=utf-8")
public class AddressController {

    @Autowired
    private AddressService addressService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取默认收货地址
     *
     * @return
     */
    @GetMapping("/default")
    public R getAddress() {
        Address address = addressService.getAddress();
        return R.success(address);
    }


    /**
     * 设置默认地址
     *
     * @return
     */
    @PutMapping("/default")
    public R setAddress(@RequestBody Address address1) {
        String id = address1.getId();
        Address address = addressService.setAddress(id);
        System.out.println(id + "==========================controller");
        return R.success(address);
    }

    /**
     * 查询地址列表
     *
     * @return
     */
    @GetMapping("/list")
    public R AddressList() {
        List<Address> addresses = addressService.selectAll();
        return R.success(addresses);
    }

    /**
     * 根据id查询信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R selectById(@PathVariable("id") String id) {
        Address address = addressService.selectById(id);
        return R.success(address);
    }

    /**
     * 新增收货地址
     *
     * @param address
     * @param request
     * @return
     */
    @PostMapping
    public R add(@RequestBody Address address, HttpServletRequest request) {
        //获取当前页面用户的电话号码作为id
        String userId = tokenToPhone(request);
        address.setUserId(userId);
        //设置创建用户
        address.setCreateUser(userId);

        addressService.add(address);
        return R.success(address);
    }


    /**
     * 删除收货地址
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delete(@Param("ids") String ids) {
        addressService.delete(ids);
        return R.success("删除成功");
    }


    /**
     * 修改收货地址
     *
     * @param address
     * @return
     */
    @PutMapping
    public R update(@RequestBody Address address, HttpServletRequest request) {
        String userId = tokenToPhone(request);
        address.setUserId(userId);
        //获取当前时间格式
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss a");
        Date date = new Date();
        address.setUpdateTime(date);
        addressService.update(address);
        return R.success("修改成功");
    }


    /**
     * 利用token获得当前页面用户名
     *
     * @param request
     * @return
     */
    public String tokenToPhone(HttpServletRequest request) {
        //用户id设置
        String token = request.getHeader("AuthorizationUser");

        String username = (String) redisTemplate.opsForValue().get(token);


        return username;
    }
}
